home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / sim.lha / sim / builtin / file.c < prev    next >
C/C++ Source or Header  |  1990-08-15  |  11KB  |  490 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* file.c */
  25.  
  26. #include  "builtin.h"
  27. #include  <netdb.h>
  28. #include  <stdio.h>
  29. #include  <sys/file.h>
  30.  
  31. #define  PMODE  0644
  32.  
  33. extern double floatval();
  34.  
  35. static int         n, a;
  36. static WORD        fileerrors = 0;   /* abort, or not on file errors */
  37. static PSC_REC_PTR user_psc_ptr, stderr_psc_ptr, psc_ptr;
  38. static FILE        *tempfile;
  39. static CHAR        s[256];
  40.  
  41. static LONG        user_word, con_word, stderr_word;
  42.  
  43. struct ftab_ent
  44. {
  45.    int   inout;      /* 1 if input, 0 if output */
  46.    LONG  p_ptr;      /* tagged ptr to psc_ptr of constant */
  47.    FILE *fdes;       /* file descriptor for this constant */
  48. };
  49.  
  50. /* table of open files; 0 is always stdin, 1 is always stdout, 
  51.    2 is always stderr */
  52. static struct ftab_ent file_table[20];
  53. static int file_tab_end = 0; /* last used entry in file_table */
  54.  
  55. /* index of current input (output) stream in file_table */
  56. static int in_file_i, out_file_i;
  57. extern FILE *curr_in, *curr_out;
  58.  
  59. static struct hostent *hp;
  60.  
  61.  
  62. get_file_index(cword, io)
  63. LONG cword;
  64. int  io;
  65. {
  66.    int i;
  67.  
  68.    for (i = 0; i <= file_tab_end; i++) {
  69.       if (file_table[i].p_ptr == cword)
  70.          if (io == file_table[i].inout || io > 1)
  71.             return i;
  72.    }
  73.    return -1;
  74. }
  75.  
  76.  
  77. b_FILEERRORS()
  78. {
  79.    fileerrors = 1;
  80. }
  81.  
  82.  
  83. b_NOFILEERRORS()
  84. {
  85.    fileerrors = 0;
  86. }
  87.  
  88.  
  89. b_PUT()  /* (N) */
  90. {
  91.    register LONG     op;
  92.    register LONG_PTR top;
  93.  
  94.    op = reg[1];  DEREF(op);
  95.    if (!ISINTEGER(op))
  96.       {FAIL0;}
  97.    else putc(INTVAL(op), curr_out);
  98. }
  99.  
  100.  
  101. b_GET0()  /* (N) */
  102. {
  103.    register LONG     op;
  104.    register LONG_PTR top;
  105.  
  106.    n = getc(curr_in);
  107.  
  108.    if (n == EOF)
  109.       clearerr(curr_in);
  110.  
  111.    op = reg[1];  DEREF(op);
  112.    if (ISNONVAR(op)) {
  113.       if (!unify(op, MAKEINT(n)))
  114.          {FAIL0;}
  115.    } else {
  116.       FOLLOW(op) = MAKEINT(n);
  117.       PUSHTRAIL(op);
  118.    }
  119. }
  120.  
  121.  
  122. b_GET()  /* (N) */
  123. {
  124.    register LONG     op;
  125.    register LONG_PTR top;
  126.  
  127.    do {
  128.      n = getc(curr_in);
  129.    } while (n != EOF && n < 16 && n >= 112);
  130.    if (n == EOF) {
  131.       clearerr(curr_in);
  132.       FAIL0;
  133.       return;
  134.    }
  135.    op = reg[1];  DEREF(op);
  136.    if (ISNONVAR(op)) {
  137.       if (!unify(op, MAKEINT(n)))
  138.          {FAIL0;}
  139.    } else {
  140.       FOLLOW(op) = MAKEINT(n);
  141.       PUSHTRAIL(op);
  142.    }
  143. }
  144.  
  145.  
  146. b_SKIP()  /* (N) */
  147. {
  148.    register LONG     op;
  149.    register LONG_PTR top;
  150.  
  151.    op = reg[1];  DEREF(op);
  152.    if (!ISINTEGER(op)) {
  153.       FAIL0;
  154.       return;
  155.    }
  156.    a = INTVAL(op);
  157.    if (a < 16 || a >= 112)
  158.       {FAIL0;}
  159.    else {
  160.       do {
  161.          n = getc(curr_in);
  162.       } while (n != EOF && n != a);
  163.       if (n = EOF) {
  164.          if (fileerrors)
  165.             quit("end of file encountered.\n");
  166.          else
  167.             {FAIL0;}
  168.       }
  169.    }
  170. }
  171.  
  172.  
  173. b_TAB()  /* (N) */
  174. {
  175.    register LONG     op;
  176.    register LONG_PTR top;
  177.  
  178.    op = reg[1];  DEREF(op);
  179.    if (!ISINTEGER(op)) {
  180.       FAIL0;
  181.       return;
  182.    }
  183.    a = INTVAL(op);
  184.    if (a < 0) {
  185.       FAIL0;
  186.       return;
  187.    }
  188.    for ( ; a > 0; a--)
  189.       putc(' ', curr_out);
  190. }
  191.  
  192.  
  193. b_NL()  /* () */
  194. {
  195.    putc('\n', curr_out);
  196.    fflush(curr_out);
  197. }
  198.  
  199.  
  200. b_WRITENAME()  /* (X) */
  201. {
  202.    register LONG     op;
  203.    register LONG_PTR top;
  204.  
  205.    op = reg[1];
  206. wnd:
  207.    switch (TAG(op)) {
  208.      case FREE: NDEREF(op, wnd);
  209.                 fprintf(curr_out, "_%d", UNTAGGED(op));
  210.                 break;
  211.      case LIST: fprintf(curr_out, ".");
  212.                 break;
  213.      case CS  : psc_ptr = GET_STR_PSC(op);
  214.                 if (IS_BUFF(psc_ptr)) 
  215.                printf("Buffer_%x", GET_NAME(psc_ptr));
  216.                 else
  217.                    writepname(curr_out, GET_NAME(psc_ptr), GET_LENGTH(psc_ptr));
  218.                 break;
  219.      case NUM : write_tnum(op, curr_out); 
  220.                 break;
  221.    }
  222. }
  223.  
  224.  
  225. b_WRITEQNAME()  /* (X) */
  226. {
  227.    register LONG     op;
  228.    register LONG_PTR top;
  229.  
  230.    op = reg[1];
  231. wnd:
  232.    switch (TAG(op)) {
  233.      case FREE: NDEREF(op, wnd);
  234.                 fprintf(curr_out, "_%d", UNTAGGED(op));
  235.                 break;
  236.      case LIST: fprintf(curr_out, ".");
  237.                 break;
  238.      case CS  : psc_ptr = GET_STR_PSC(op);
  239.                 if (IS_BUFF(psc_ptr))
  240.                printf("Buffer_%x", GET_NAME(psc_ptr));
  241.                 else
  242.                    writeqname(curr_out, GET_NAME(psc_ptr), GET_LENGTH(psc_ptr));
  243.                 break;
  244.      case NUM : write_tnum(op, curr_out);
  245.                 break;
  246.    }
  247. }
  248.  
  249.  
  250. b_RESET()  /* () */
  251. {
  252.    quit("RESET not implemented\n");
  253.  
  254. /*
  255.    fop = reg[1];
  256.    get_file_psc();
  257.    if (p == user_psc_ptr)
  258.       set_file_ptr(p, stdin);
  259.    else {
  260.        namestring(p, s);
  261.        set_file_ptr(p, fopen(s, "r"));
  262.        if (get_file_ptr(p) == 0)
  263.           {FAIL0;}
  264.    }
  265. */
  266. }
  267.  
  268.  
  269. b_REWRITE()
  270. {
  271.    quit("REWRITE not implemented\n");
  272. /*
  273.    fop = reg[1];
  274.    get_file_psc();
  275.    if (p == user_psc_ptr)
  276.       set_file_ptr(p, stdout);
  277.    else {
  278.       namestring(p, s);
  279.       set_file_ptr(p, fopen(s, "w"));
  280.       if (get_file_ptr(p) == 0)
  281.          {FAIL0;}
  282.    }
  283. */
  284. }
  285.  
  286.  
  287. b_CLOSE()
  288. {
  289.    register LONG     fop;
  290.    register LONG_PTR top;
  291.    int     i;
  292.  
  293.    fop = reg[1];  DEREF(fop);
  294.    i = get_file_index(fop, 2);
  295.    if (i > 2) {    /* not user, stderr */
  296.       fclose(file_table[i].fdes);
  297.       for ( ; i < file_tab_end; i++)
  298.          file_table[i] = file_table[i+1];
  299.       file_tab_end--;
  300.    }
  301. }
  302.  
  303.  
  304. b_SEE()  /* reg1: file name */
  305. {
  306.    register LONG     fop;
  307.    register LONG_PTR top;
  308.    int      temp_in_file_i;
  309.  
  310.    fop = reg[1];  DEREF(fop);
  311.    temp_in_file_i = get_file_index(fop, 1);
  312.    if (temp_in_file_i < 0) {    /* not in table */
  313.        namestring(GET_STR_PSC(fop), s);
  314.        tempfile = fopen(s, "r");
  315.        if (!tempfile) {
  316.           FAIL0;          /* leaving in_file_i unchanged */
  317.           return;
  318.        }
  319.        in_file_i = ++file_tab_end;
  320.        file_table[in_file_i].inout = 1;
  321.        file_table[in_file_i].p_ptr = fop;
  322.        file_table[in_file_i].fdes  = tempfile;
  323.    }
  324.    else in_file_i = temp_in_file_i;    /* take it from table */
  325.    curr_in = file_table[in_file_i].fdes;
  326. }
  327.  
  328.  
  329. b_TELL()  /* reg1: file name */
  330. {         /* reg2: 0 -> open `w'-write; 1 -> open `a'-append */
  331.  
  332.    register LONG     sop, fop;
  333.    register LONG_PTR top;
  334.  
  335.    fop = reg[1];  DEREF(fop);
  336.    sop = reg[2];  DEREF(sop);
  337.    out_file_i = get_file_index(fop, 0);
  338.    if (out_file_i < 0) {                 /* not in table */
  339.        namestring(GET_STR_PSC(fop), s);
  340.        if(INTVAL(sop))
  341.           tempfile = fopen(s, "a");
  342.        else
  343.           tempfile = fopen(s, "w");
  344.        if (!tempfile) {
  345.           FAIL0;
  346.           return;
  347.        }
  348.        out_file_i = ++file_tab_end;
  349.        file_table[out_file_i].inout = 0;
  350.        file_table[out_file_i].p_ptr = fop;
  351.        file_table[out_file_i].fdes  = tempfile;
  352.    }
  353.    curr_out = file_table[out_file_i].fdes;
  354. }
  355.  
  356.  
  357. b_SEEING()  /* reg1: unified with the current input file name */
  358. {
  359.    if (!unify(reg[1], file_table[in_file_i].p_ptr))
  360.       {FAIL0;}
  361. }
  362.  
  363.  
  364. b_TELLING()  /* reg1: unified with the current out put file name */
  365. {
  366.    if (!unify(reg[1], file_table[out_file_i].p_ptr))
  367.       {FAIL0;}
  368. }
  369.  
  370.  
  371. b_SEEN()
  372. {
  373.    if (in_file_i > 2) {
  374.       fclose(curr_in);
  375.       for ( ; in_file_i < file_tab_end; in_file_i++)
  376.           file_table[in_file_i] = file_table[in_file_i+1];
  377.       file_tab_end--;
  378.    }
  379.    in_file_i = 0;    /* reset to user */
  380.    curr_in = file_table[in_file_i].fdes;
  381. }
  382.  
  383.  
  384. b_TOLD()
  385. {
  386.    if (out_file_i > 2) {
  387.       fclose(file_table[out_file_i].fdes);
  388.       for ( ; out_file_i < file_tab_end; out_file_i++)
  389.          file_table[out_file_i] = file_table[out_file_i+1];
  390.       file_tab_end--;
  391.    }
  392.    out_file_i = 1;       /* reset to user */
  393.    curr_out = file_table[out_file_i].fdes;
  394. }
  395.  
  396. static BYTE perm = PERM;
  397.  
  398. file_init()
  399. {
  400.     LONG temp;
  401.     CHAR arity = 0;
  402.  
  403.     temp = insert("user", 4, arity, &perm);
  404.     user_psc_ptr = (PSC_REC_PTR)FOLLOW(temp);
  405.     user_word = temp | CS_TAG;
  406.  
  407.     temp = insert("stderr", 6, arity, &perm);
  408.     stderr_psc_ptr = (PSC_REC_PTR)(FOLLOW(temp));
  409.     stderr_word = temp | CS_TAG;
  410.  
  411.     file_table[0].inout = 1;
  412.     file_table[0].p_ptr = user_word;
  413.     file_table[0].fdes  = stdin;
  414.     in_file_i = 0;
  415.     curr_in   = stdin;
  416.  
  417.     file_table[1].inout = 0;
  418.     file_table[1].p_ptr = user_word;
  419.     file_table[1].fdes  = stdout;
  420.     out_file_i = 1;
  421.     curr_out   = stdout;
  422.  
  423.     file_table[2].inout = 0;
  424.     file_table[2].p_ptr = stderr_word;
  425.     file_table[2].fdes = stderr;
  426.  
  427.     file_tab_end = 2;
  428. }
  429.  
  430.  
  431. b_ACCESS()
  432. {
  433.     register LONG     op1, op2;
  434.     int mode, r;
  435.     char name[256], *s;
  436.     register LONG_PTR top;
  437.     PSC_REC_PTR psc_ptr;
  438.  
  439.     op1 = reg[1]; DEREF(op1);
  440.     op2 = reg[2]; DEREF(op2);
  441.  
  442.     if (ISATOM(op1)) {
  443.       psc_ptr = GET_STR_PSC(op1);
  444.       if (IS_ORDI(psc_ptr)) {
  445.     namestring(psc_ptr, name);
  446.     s = name;
  447.       }
  448.       else if (IS_BUFF(psc_ptr)) s = GET_NAME(psc_ptr);
  449.       else {
  450.     printf("access: illegal first argument\n");
  451.     FAIL0;
  452.       }
  453.     };
  454.     if (ISINTEGER(op2)) mode = INTVAL(op2);
  455.     else {
  456.       fprintf(stderr, "access: second argument must be an integer\n");
  457.       FAIL0;
  458.     };
  459.     r = access(s, mode);
  460.     if (!unify(reg[3], MAKEINT(r))) {FAIL0;}
  461. }
  462.  
  463.  
  464. b_WRITE4() 
  465. {   /* reg1 contains a bit string that is written out in 4 bytes */
  466.  
  467.     register LONG     op, wbyte;
  468.     register LONG_PTR top;
  469.     WORD     i;
  470.  
  471.     op = reg[1];  DEREF(op);
  472.     for (i = 1; i <= 4; i++) {
  473.        wbyte = ((op & 0xff000000) >> 24);
  474.        op = op << 8;
  475.        putc(wbyte, curr_out);
  476.     }
  477. }
  478.  
  479.  
  480. b_WRITEPTR()
  481. {
  482.     register LONG     op;  
  483.     register LONG_PTR top;
  484.  
  485.     op = reg[1];  DEREF(op);
  486.  
  487.     printf("%08x", op & ~ CS_TAG);
  488. }
  489.  
  490.